package com.jml.spring.aop事务.service.impl;


import com.jml.spring.aop事务.TransactionUtils.TransactionUtils;
import com.jml.spring.aop事务.dao.UserDao;
import com.jml.spring.aop事务.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;

//user 服务层
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private TransactionUtils transactionUtils;

	// spring 事务封装呢？ aop技术
	//使用aop重构
	public void add() {
		int i=10;
		userDao.add("jmll",i);
		int j=1/0;
		System.out.println("--#########往数据库添加数据...");
		userDao.add("jmll",i+1);
	}

	//不使用aop
	public void add001() {
		TransactionStatus transactionStatus = transactionUtils.begin();
		try {
			int i=10;
			userDao.add("jmll",i);
			//int j=1/0;
			System.out.println("#########往数据库添加数据...");
			userDao.add("jmll",i+1);
			if(transactionStatus!=null){
				transactionUtils.commit(transactionStatus);
			}
		} catch (Exception e) {
			if(transactionStatus!=null){
				transactionUtils.rollback(transactionStatus);
			}
		}
	}

}
